<script src="/js/portal/jquery.js"></script>
<script src="/js/portal/jquery-ui-1.8.16.custom.min.js"></script>
<script src="/js/portal/jquery.mjs.nestedSortable.js"></script>
<script>
 $(document).ready(function(){

        $('.sortable').nestedSortable({
            handle: 'div',
            items: 'li',
            toleranceElement: '> div'
        });

    });
    
    
    function saveMenuList()
    {
      var listArray;
      ///arraied = $('ol.sortable').nestedSortable('toArray', {startDepthCount: 0});
      arraied = $('ol.sortable').nestedSortable('serialize');
      //arraied = dump(arraied);
      //console.log(arraied);
      
      //list = $('ol.sortable').nestedSortable('toHierarchy', {startDepthCount: 0});
      
       $.ajax({
            type: 'post',
            url: "{{ path('saveMenuList') }}",            
            data: arraied,
            success: function (msg) {              
              $('#mainMenuPanel').load("{{ path('menuNameList') }}");
            }
          });
    }
        
    function dump(arr,level) 
    {
		var dumped_text = "";
		if(!level) level = 0;

		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";

		if(typeof(arr) == 'object') { //Array/Hashes/Objects
			for(var item in arr) {
				var value = arr[item];

				if(typeof(value) == 'object') { //If it is an array,
					dumped_text += level_padding + "'" + item + "' ...\n";
					dumped_text += dump(value,level+1);
				} else {
					dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
				}
			}
		} else { //Strings/Chars/Numbers etc.
			dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		}
		return dumped_text;
	}
    
</script>

{% include 'AdminPanelMenuBundle:Menu:menu.css.twig' %}


<ol class="sortable">
  {% for key, value in entities %}
    <li id="list_{{value.id}}">
      <div><a href="{{ path('menu_edit', { 'id': value.id }) }}">{{ value.name_mn }}</a></div>
      {% if value.child is null %}
      {% else %}
        <ol>
          {% for chaildKey, childValue in value.child %}
            <li id="list_{{childValue.id}}">
              <div><a href="{{ path('menu_edit', { 'id': childValue.id }) }}">{{ childValue.name_mn }}</a></div>
            </li>
          {% endfor %}
        </ol>
      {% endif %}
    </li>
  {% else %}
  {% endfor %}
</ol>
  
<button class="btn btn-block btn-success btn-icon glyphicons ok" onclick="saveMenuList()" style="width: 120px;text-align: center;padding-left: 5px;margin-bottom: 20px">Хадгалах</button> / Менью байршил хадгалах /
